package cn.lg.soar.cms.biz.modules.other.model;

import cn.lg.soar.common.util.data.DataUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2025/1/10 12:16
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class RegionTreeCache {

    private Integer value;

    private String label;

    private Boolean disabled;

    private List<RegionTreeCache> children;

    public RegionTreeCache() {
    }

    public RegionTreeCache(Integer value, String label, Boolean disabled) {
        this(value, label, disabled, null);
    }

    public RegionTreeCache(Integer value, String label, Boolean disabled, List<RegionTreeCache> children) {
        this.value = value;
        this.label = label;
        this.disabled = Boolean.TRUE.equals(disabled) ? true : null;
        this.children = DataUtil.isEmpty(children) ? null : children;
    }

    public void setValue(Integer value) {
        if (this.value != null) {
            throw new RuntimeException("缓存数据不支持修改");
        }
        this.value = value;
    }

    public void setLabel(String label) {
        if (this.label != null) {
            throw new RuntimeException("缓存数据不支持修改");
        }
        this.label = label;
    }

    public void setDisabled(Boolean disabled) {
        if (this.disabled != null) {
            throw new RuntimeException("缓存数据不支持修改");
        }
        this.disabled = disabled;
    }

    public void setChildren(List<RegionTreeCache> children) {
        if (this.children != null) {
            throw new RuntimeException("缓存数据不支持修改");
        }
        this.children = children;
    }

    public Integer getValue() {
        return value;
    }

    public String getLabel() {
        return label;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public List<RegionTreeCache> getChildren() {
        return children;
    }

    /**
     * 根据id获取子级树
     * @param tree
     * @param id
     * @return
     */
    public static List<RegionTreeCache> findChildren(List<RegionTreeCache> tree, int id) {
        for (RegionTreeCache x : tree) {
            if (Objects.equals(x.getValue(), id)) {
                if (x.children != null) {
                    return x.children;
                }
                return Collections.emptyList();
            }
            if (DataUtil.isEmpty(x.children)) {
                continue;
            }
            List<RegionTreeCache> children = findChildren(x.children, id);
            if (children != null) {
                return children;
            }
        }
        return null;
    }

    /**
     * 查找元素链
     * @param tree
     * @param predicate
     * @return
     */
    public static List<RegionTreeCache> findLink(List<RegionTreeCache> tree, Predicate<RegionTreeCache> predicate) {
        if (tree != null) {
            for (RegionTreeCache t : tree) {
                if (predicate.test(t)) {
                    List<RegionTreeCache> result = new ArrayList<>(tree.size());
                    result.add(t);
                    return result;
                }
                List<RegionTreeCache> link = findLink(t.getChildren(), predicate);
                if (!link.isEmpty()) {
                    link.add(t);
                    return link;
                }
            }
        }
        return Collections.emptyList();
    }

}
